package com.example.cunion.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.cunion.common.R;
import com.example.cunion.config.shiro.JwtUtil;
import com.example.cunion.controller.form.*;
import com.example.cunion.entity.Shop;
import com.example.cunion.service.ShopService;
import com.example.cunion.util.StringSnowflakeIdGenerator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Resource
    private ShopService shopService;

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private JwtUtil jwtUtil;
    /**
     * 查询所有商家
     */
    @PostMapping("/searchAllShopsByPage")
    public R searchAllShopsByPage(@RequestHeader("token") String token, @RequestBody ShopForm shopForm){
        //获取分页信息
        int start = shopForm.getStart();
        int length = shopForm.getLength();
        //获取位置信息
        String position = shopForm.getPosition();
        //获取楼层信息
        Integer floor = shopForm.getFloor();
        //获取搜索信息
        String searchValue = shopForm.getSearchValue();
        //计算起始位置
        start = (start - 1) * length;
        //创建HashMap
        HashMap hashMap = new HashMap();
        //将分页信息放入HashMap
        hashMap.put("start", start);
        hashMap.put("length", length);
        hashMap.put("position", position);
        hashMap.put("floor", floor);
        hashMap.put("searchValue", searchValue);
        //调用查询方法
        List<HashMap> maps = shopService.searchAllShops(hashMap);
        //返回查询结果
        return R.ok().put("result", maps);
    }

    /**
     * 查询所有商家
     */
    @PostMapping("/searchAllShops")
    public R searchAllShops(@RequestHeader("token") String token, @RequestBody ShopForm shopForm){
        //获取分页信息
        int start = shopForm.getStart();
        int length = shopForm.getLength();
        //获取位置信息
        String position = shopForm.getPosition();
        //获取楼层信息
        Integer floor = shopForm.getFloor();
        //获取搜索信息
        String searchValue = shopForm.getSearchValue();
        //计算起始位置
        start = (start - 1) * length;
        //创建HashMap
        HashMap hashMap = new HashMap();
        //将分页信息放入HashMap
        hashMap.put("start", start);
        hashMap.put("length", length);
        hashMap.put("position", position);
        hashMap.put("floor", floor);
        hashMap.put("searchValue", searchValue);
        //调用查询方法
        List<HashMap> maps = shopService.searchAllShopsByPage(hashMap);
        //返回查询结果
        return R.ok().put("result", maps);
    }

    /**
     * 通过id查询商家
     */
    @PostMapping("/searchShopById")
    public R searchShopById(@RequestHeader("token") String token, @RequestBody SingleShopForm singleShopForm){
        //获取商家id
        String shopId = singleShopForm.getShopId();
        //调用查询方法
        HashMap map = shopService.searchShopById(shopId);
        //返回查询结果
        return R.ok().put("result", map);
    }


    @PostMapping("/updateShopById")
    @RequiresPermissions(value = {"admin"})
    public R updateShopById(@RequestHeader("token") String token, @RequestBody UpdateShopByIdForm form){
        //将表单转换为HashMap
        HashMap<String, Object> map = (HashMap<String, Object>) BeanUtil.beanToMap(form);
        //调用更新方法
        Integer result = shopService.updateShopById(map);
        //返回更新结果
        return R.ok();
    }

    @GetMapping("/deleteShop")
    public R deleteShop(@RequestHeader("token") String token, @RequestParam("shopId") String shopId){
        //调用删除方法
        Integer result = shopService.deleteShop(shopId);
        //返回删除结果
        return R.ok();
    }

    @PostMapping("/addShop")
    public R addShop(@RequestHeader("token") String token, @RequestBody AddShopForm form){
        String userId = jwtUtil.getUserId(token);
        StringSnowflakeIdGenerator stringSnowflakeIdGenerator = new StringSnowflakeIdGenerator(1, 1);
        String id = stringSnowflakeIdGenerator.nextId();
        form.setId(id);
        form.setUserId(userId);
        DecimalFormat decimalFormat = new DecimalFormat("#.0");
        String formattedNumber = decimalFormat.format(1.0);
        form.setShopScore(formattedNumber);
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = shopService.addShop((HashMap) map);
        return R.ok();
    }

    @PostMapping("/searchUnCheckShopByPage")
    public R searchUnCheckShopByPage(@RequestHeader("token") String token, @RequestBody SearchUnCheckShopByPageForm form){
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap map = new HashMap();
        map.put("start", start);
        map.put("length", length);
        List<HashMap> list = shopService.searchUnCheckShopByPage(map);
        return R.ok().put("result", list);
    }

    @GetMapping("/agreeShop")
    public R agreeShop(@RequestHeader("token") String token, @RequestParam("id") String id){
        Integer result = shopService.agreeShop(id);
        return R.ok();
    }

    @GetMapping("/rejectShop")
    public R rejectShop(@RequestHeader("token") String token, @RequestParam("id") String id){
        Integer result = shopService.rejectShop(id);
        return R.ok();
    }

    @PostMapping("/updateShopStatus")
    public R updateShopStatus(@RequestHeader("token") String token, @RequestBody UpdateShopStatusForm form){
        Map<String, Object> map = BeanUtil.beanToMap(form);
        if (form.getStatus().equals("已通过")){
            Set keys = redisTemplate.keys("shop:searchAllShops:*");
            if (keys != null){
                redisTemplate.delete(keys);
            }
        }
        Integer result = shopService.updateShopStatus((HashMap) map);
        return R.ok();
    }

    @PostMapping("/searchComplianceShopByPage")
    public R searchComplianceShopByPage(@RequestHeader("token") String token, @RequestBody SearchComplianceShopByPageForm form){
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", start);
        map.put("length", length);
        List<HashMap> list = shopService.searchComplianceShopByPage(map);
        return R.ok().put("result", list);
    }

    @PostMapping("/addShopByMp")
    public R addShopByMp(@RequestHeader("token") String token, @RequestBody Shop shop) {
        shopService.addShopByMp(shop);
        return R.ok();
    }

    @GetMapping("/deleteShopByMp")
    public R deleteShopByMp(@RequestHeader("token") String token, @RequestParam("shopId") String shopId) {
        shopService.deletedShopByMp(shopId);
        return R.ok();
    }
    @PostMapping("/updateShopByMp")
    public R updateShopByMp(@RequestHeader("token") String token, @RequestBody Shop shop) {
        shopService.updateShopByMp(shop);
        return R.ok();
    }
    @PostMapping("/searchShopByMp")
    public R searchShopByMp(@RequestHeader("token") String token, @RequestBody PageFom form){
        List<Shop> list = shopService.searchShopByMp(form.getStart(), form.getLength());
        return R.ok().put("result", list);
    }
}
